package com.ijava.base.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ijava.base.domain.DictionaryDefine;
import com.ijava.base.domain.DictionaryDetail;
import com.ijava.base.domain.Position;
import com.ijava.base.domain.User;
import com.ijava.base.service.DictionaryDefineService;
import com.ijava.base.service.DictionaryDetailService;

@Controller
@RequestMapping("/admin/dictionaryDefine.do")
@SessionAttributes("list")
public class DictionaryDefineController {
	private static final Logger logger = LoggerFactory.getLogger(DictionaryDefineController.class);
	@Autowired
	private DictionaryDefineService service;
	@Autowired
	private DictionaryDetailService detailService;

	@RequestMapping(params = "action=list")
	public String list(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session)
			throws Exception {
		try {
			List list = service.findWithPaging(dictionaryDefine);
			modelMap.addAttribute("list", list);
			modelMap.addAttribute("command", dictionaryDefine);
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDefineList.jsp";
	}

	@RequestMapping(params = "action=new")
	public String neu(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session)
			throws Exception {
		try {
			modelMap.addAttribute("command", dictionaryDefine);
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDefineSaveEdit.jsp";
	}

	@RequestMapping(params = "action=edit")
	public String edit(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session) throws Exception {
		try {
			if (StringUtils.hasLength(dictionaryDefine.getDefineId())) {
				modelMap.addAttribute("command", service.get(dictionaryDefine.getDefineId()));
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("command", dictionaryDefine);
		}
		return "forward:/admin/dictionary/dictionaryDefineSaveEdit.jsp";
	}

	@RequestMapping(params = "action=remove")
	public String remove(ModelMap modelMap, String defineId, HttpSession session)
			throws Exception {
		modelMap.addAttribute("code","dictionaryDefine.remove.success");
		try {
//			List list = new ArrayList();
			List list = detailService.findChild(new DictionaryDefine(defineId));
//			detailService.findTree(defineId, list);
			if(list.size() == 0){
				service.remove(defineId);
			} else {
				modelMap.addAttribute("code","cant.delete.node.has.child");
				modelMap.addAttribute("isError","true");
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("code","dictionaryDefine.remove.failed");
			modelMap.addAttribute("isError","true");
		}
		return list(modelMap, new DictionaryDefine(), session);
	}

	@RequestMapping(params = "action=mutilRemove")
	public String mutilRemove(ModelMap modelMap, DictionaryDefine dictionaryDefine,
			HttpSession session) throws Exception {
		modelMap.addAttribute("code","dictionaryDefine.remove.success");
		try {
			
			List list = detailService.findChild(dictionaryDefine);
			
			if(list.size() == 0){
				service.mutilRemove(dictionaryDefine);
			} else {
				modelMap.addAttribute("code","cant.delete.node.has.child");
				modelMap.addAttribute("isError","true");
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("code","dictionaryDefine.remove.failed");
			modelMap.addAttribute("isError","true");
		}
		return list(modelMap, new DictionaryDefine(), session);
	}

	@RequestMapping(params = "action=saveEdit")
	public String saveEdit(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session)
			throws Exception {
		String dictionaryDefineId = dictionaryDefine.getDefineId();
		try {
			User loginUser = (User) session.getAttribute("loginUser");

			if (dictionaryDefineId == null || dictionaryDefineId.length() == 0) {
				modelMap.addAttribute("code","dictionaryDefine.new.success");
				dictionaryDefine.setCrUserId(loginUser.getUserId());
				dictionaryDefine.setCrTime(new Date(System.currentTimeMillis()));
				service.save(dictionaryDefine);
				if("saveAndNew".equals(dictionaryDefine.getSaveAndNew())){
					return neu(modelMap, new DictionaryDefine(), session);
				} else {
					return edit(modelMap, dictionaryDefine,session);
				}
			} else {
				modelMap.addAttribute("code","dictionaryDefine.modify.success");
				dictionaryDefine.setUpUserId(loginUser.getUserId());
				dictionaryDefine.setUpTime(new Date(System.currentTimeMillis()));
				service.update(dictionaryDefine);
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("isError","true");
			if (dictionaryDefineId == null || dictionaryDefineId.length() == 0) {
				modelMap.addAttribute("code","dictionaryDefine.new.failed");
				modelMap.addAttribute("command", dictionaryDefine);
				return neu(modelMap, dictionaryDefine,session);
			} else {
				modelMap.addAttribute("code","dictionaryDefine.modify.failed");
				return edit(modelMap, dictionaryDefine,session);
			}
		}
		return list(modelMap, new DictionaryDefine(), session);
	}
}

